package com.zzt.gson.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * @author: zeting
 * @date: 2022/11/16
 */
public class GsonUtil {

    private Gson gson;

    private static volatile GsonUtil instance;

    public GsonUtil() {
        if (gson == null) {
            GsonBuilder gsonBuilder = new GsonBuilder();
            //注册自定义String的适配器
            gsonBuilder.registerTypeAdapter(String.class, new StringTypeAdapter());
            gson = gsonBuilder.create();
        }
    }

    public static GsonUtil getInstance() {
        if (instance == null) {
            synchronized (GsonUtil.class) {
                if (instance == null) {
                    instance = new GsonUtil();
                }
            }
        }
        return instance;
    }

    public Gson getGson() {
        return gson;
    }

}
